<?php

namespace App\V3\V3_2_6\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class MaterielContentModel extends Model
{


    use HasFactory;
    protected $table = 'materiel_content';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    protected $dates = [
        'purchase_time'
    ];
    /**
     * toArray 时间转换问题
     * change 在 2021/10/14 14:58 创建
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getInfo($materielContentId)
    {
        return self::select(
            'a.id',
            'a.type_id',
            'a.rest_num',
            'b.materiel_name',
            'b.type_name',
            'b.materiel_no',
            'b.materiel_img',
            'c.id as second_class_id',
            'c.class_name as second_class_name',
            'c.class_no as second_class_no',
            'd.id as first_class_id',
            'd.class_name as first_class_name',
            'e.purchase_no',
            'b.is_product_materiel'
        )
            ->from('materiel_content as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
            ->leftJoin('materiel_class as c', 'c.id', '=', 'b.materiel_class_id')
            ->leftJoin('materiel_class as d', 'd.id', '=', 'c.parent_id')
            ->leftJoin('materiel as e', 'e.id', '=', 'a.materiel_id')
            ->where('a.id', $materielContentId)
            ->first();
    }

    static public function getMaterielId($condition, $userId)
    {
        return self::select('materiel_id')->from('materiel_content as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
            ->where($condition)->where($userId)->get()->toArray();
    }
    //今日采购金额
    static public function getTodayMaterielMoney($condition, $startTime, $endTime){
        $info = MaterielModel::select(DB::raw('SUM(final_money) as total_price'))
            ->where($condition)
            ->where('pay_status','!=',0)
            ->where('in_storage_status','!=',0)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->first();

        if(!empty($info['total_price'])){
            return $info['total_price'];
        }else{
            return '0.00';
        }
    }
}
